package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.common.MD5Util;
import cn.cs.pojo.CsFactory;
import cn.cs.service.CsFactoryService;
import cn.cs.service.CsPartsService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/factoryLogin")
public class FactoryLoginController {

    @Resource
    private CsFactoryService csFactoryService;


    @Resource
    private CsPartsService csPartsService;

    /**
     * 配件厂登陆验证
     * @param csFactory
     * @return
     */
    @PostMapping(path = "/csPlatform/loginVerity")
    public Dto loginVerity(@RequestBody CsFactory csFactory, HttpServletRequest request)throws Exception{
        String username = csFactory.getFactoryCode();
        String password = csFactory.getFactoryPassword();
        CsFactory factory = csFactoryService.getCsFactoryByUserName(username);
        if(EmptyUtils.isEmpty(factory)){
            return DtoUtil.returnFail("用户名不存在","001");
        }
        if(!MD5Util.encode(password).equals(factory.getFactoryPassword())){
            return DtoUtil.returnFail("密码错误","002");
        }
        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(60*60);
        session.setAttribute("factory", factory);
        return DtoUtil.returnSuccess("登陆成功", factory);
    }


    /**
     * 判断是否登陆状态
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/loginJudge")
    public Dto loginJudge(HttpServletRequest request)throws Exception{
        HttpSession session = request.getSession();
        Object factory = session.getAttribute("factory");
        if(EmptyUtils.isNotEmpty(factory)){
            return DtoUtil.returnSuccess("获取成功", "");
        }else{
            return DtoUtil.returnFail("用户未登录!", "001001001");
        }
    }


    /**
     * 配件厂退出
     * @return
     */
    @PostMapping(path = "/csPlatform/loginOut")
    public Dto loginOut(HttpServletRequest request)throws Exception{
        request.getSession().removeAttribute("factory");
        return DtoUtil.returnSuccess("退出成功", "");
    }


    /**
     * 修改密码
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateFactoryPassword")
    public Dto updateFactoryPassword(String username, String password)throws Exception{
        CsFactory factory = csFactoryService.getCsFactoryByUserName(username);
        if(EmptyUtils.isEmpty(factory)){
            return DtoUtil.returnFail("用户名不存在","001");
        }
        factory.setFactoryPassword(MD5Util.encode(password));
        factory.setModifyDate(new Date());
        csFactoryService.modifyCsFactory(factory);
        return DtoUtil.returnSuccess("修改成功", "");

    }


    /**
     * 分页查询 配件信息
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getPartsByPage")
    public Dto getPartsByPage(HttpServletRequest request, Integer pageNumber, Integer pageSize, String partsName, String partsNumber, String factoryId)throws Exception{
        HttpSession session = request.getSession();
        Object factory = session.getAttribute("factory");
        if(EmptyUtils.isNotEmpty(factory)){
            Map<String, Object> param = new HashMap<>();
            param.put("partsName", partsName);
            param.put("partsNumber", partsNumber);
            param.put("factoryId", factoryId);
            return DtoUtil.returnSuccess("获取成功", csPartsService.queryCsPartsPageByMap(param, pageNumber, pageSize));
        }else{
            return DtoUtil.returnFail("用户未登录!", "001");
        }
    }


    /**
     * 批量生成二维码
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/csPlatform/batchQRCode")
    public Dto batchQRCode(HttpServletRequest request, HttpServletResponse response){
//        csPartsService.downloadQrCode(request, response);
        return csPartsService.saveQrCodeData(request, response);
    }



    /**
     * 下载二维码Excel poi
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/downloadExcel")
    public void downloadExcelPrint(HttpServletRequest request, HttpServletResponse response)throws Exception{
        csPartsService.downloadExcelPrint(request, response);
    }



    /**
     * 批量新增出厂日期
     * @param partsId
     * @param QRcode
     * @param startCode
     * @param endCode
     * @param productionDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/productDateNew")
    public Dto productDateNew(String partsId, String QRcode, String startCode, String endCode, String productionDate)throws Exception{
        Map<String, Object> param = new HashMap<>();
        param.put("productionDate", productionDate);
        param.put("partsId", partsId);
        param.put("QRcode", QRcode);
        param.put("startCode", startCode);
        param.put("endCode", endCode);
        return DtoUtil.returnSuccess("操作成功", csPartsService.updateCsPartsQRcode(param));
    }


    /**
     * 查询配件二维码信息
     * @param pageNumber
     * @param pageSize
     * @param partsId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getPartsQRCodeByPage")
    public Dto getPartsQRCodeByPage(Integer pageNumber, Integer pageSize, String partsId)throws Exception{
        Map<String, Object> param = new HashMap<>();
        param.put("partsId", partsId);
        return DtoUtil.returnSuccess("获取成功", csPartsService.queryCsPartsQRcodePageByMap(param, pageNumber, pageSize));
    }
//=========TODO
    /**
     * 根据二维码查询此产品操作记录
     * @param QRcode
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getHistoryByQRcode")
    public Dto getHistoryByQRcode(String QRcode)throws Exception{
        return DtoUtil.returnSuccess("获取成功", csPartsService.getHistoryByQRcode(QRcode));
    }


    /**
     * 根据二维码查询商品信息，以及操作记录（扫描二维码）
     * @param QRcode
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getPartsByQRcode")
    public Dto getPartsByQRcode(String QRcode)throws Exception{
        Map<String, Object> map = csPartsService.getPartsByQRcode(QRcode);
        map.put("list", csPartsService.getHistoryByQRcode(QRcode));
        return DtoUtil.returnSuccess("获取成功", map);
    }






}
